﻿using System;
using System.Collections.Generic;
using System.Text;
using AMS.DataAccess.Properties;
using System.Reflection;

namespace AMS.DataAccess.Sql
{
    /// <summary>
    /// 用以构造DELETE的SQL语句
    /// </summary>
    public class SqlDelete : SqlStatement, IConditionProvider<SqlDelete>
    {
        /// <summary>
        /// 用以构造DELETE的SQL语句
        /// </summary>
        /// <param name="tableName">表名</param>
        public SqlDelete(string tableName)
        {
            this.tableName = tableName;
            sbAppendix = new StringBuilder();
            conditionBuilder = new ConditionBuilder();
        }

        /// <summary>
        /// 用以构造DELETE的SQL语句
        /// </summary>
        /// <param name="tableName">表名</param>
        /// <param name="compactMode">是否紧凑模式</param>
        public SqlDelete(string tableName, bool compactMode)
            : this(tableName)
        {
            CompactMode = compactMode;
        }

        #region Fields & Properties
        /// <summary>
        /// 表名
        /// </summary>
        private string tableName;

        /// <summary>
        /// 附加内容
        /// </summary>
        private StringBuilder sbAppendix;

        /// <summary>
        /// 获取表名
        /// </summary>
        public string TableName
        {
            get { return tableName; }
        }

        /// <summary>
        /// 条件表达式构造器
        /// </summary>
        ConditionBuilder conditionBuilder;

        /// <summary>
        /// 获取或设置一个值，表示是否紧凑模式。如果是紧凑模式，则尽量将SQL语句构造在一行以内。
        /// </summary>
        public override bool CompactMode
        {
            get { return base.CompactMode; }
            set
            {
                base.CompactMode = value;
                conditionBuilder.CompactMode = value;
            }
        }
        #endregion

        /// <summary>
        /// 将对象的主键添加为 where 条件
        /// </summary>
        /// <param name="businessObject">业务对象</param>
        public void AddPrimaryKeyFieldAsCondition(object businessObject)
        {
            conditionBuilder.AddPrimaryKeyFieldAsCondition(businessObject);
        }

        /// <summary>
        /// 将对象的主键添加为 where 条件
        /// </summary>
        /// <param name="businessObject">业务对象</param>
        /// <param name="objectType">对象的实际类型</param>
        public void AddPrimaryKeyFieldAsCondition(object businessObject, Type objectType)
        {
            conditionBuilder.AddPrimaryKeyFieldAsCondition(businessObject, objectType);
        }

        /// <summary>
        /// 添加条件
        /// </summary>
        /// <param name="condition">条件表达式</param>
        public SqlDelete AddCondition(string condition)
        {
            conditionBuilder.AddCondition(condition);
            return this;
        }

        /// <summary>
        /// 添加条件
        /// </summary>
        /// <param name="condition">条件表达式</param>
        public SqlDelete AddCondition(SqlCondition condition)
        {
            conditionBuilder.AddCondition(condition);
            return this;
        }

        /// <summary>
        /// 添加条件
        /// </summary>
        /// <param name="sql">条件表达式的前半部</param>
        /// <param name="sql2">条件表达式的后半部</param>
        public SqlDelete AddCondition(string sql, string sql2)
        {
            conditionBuilder.AddCondition(sql, sql2);
            return this;
        }

        /// <summary>
        /// 添加表达式
        /// </summary>
        /// <param name="logicOperator">与现有内容的条件表达式，必需是 AND/OR, 且需要管理空格</param>
        /// <param name="condition">条件</param>
        public SqlDelete AddCondition(string logicOperator, SqlCondition condition)
        {
            conditionBuilder.AddCondition(logicOperator, condition);
            return this;
        }

        /// <summary>
        /// 获取条件部分
        /// </summary>
        /// <returns>条件表达式</returns>
        public string GetCondition()
        {
            return conditionBuilder.ToString();
        }

        /// <summary>
        /// 转换成SQL语句
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            StringBuilder sbSql = new StringBuilder();
            sbSql.Append("DELETE FROM ");
            NL(sbSql.Append(tableName));

            if (sbAppendix.Length > 0)
            {
                sbSql.Append(sbAppendix.ToString());
            }

            if (conditionBuilder.Condition.Length > 0)
            {
                if (CompactMode)
                {
                    sbSql.Append(" WHERE ");
                    sbSql.Append(conditionBuilder.Condition.ToString());
                }
                else
                {
                    sbSql.AppendLine("WHERE");
                    sbSql.Append(conditionBuilder.Condition.ToString());
                }
            }

            return sbSql.ToString();
        }
    }
}
